package LockSupport与线程中断.interrupt;

import java.util.concurrent.TimeUnit;

/**
 * @program: algorithm
 * @description: 通过线程自带api停止一个中断的线程
 * @author: 陈恩涛
 * @create: 2022-08-12 07:26
 **/
public class InterruptDemo3 {

    public static void main(String[] args) {

        final Thread t1 = new Thread(() -> {
            while (true) {
                if (Thread.currentThread().isInterrupted()) {
                    System.out.println(Thread.currentThread().getName() + "\t 中断标志为被修改为true,线程终止！");
                    break;
                }
                try {
                    TimeUnit.MILLISECONDS.sleep(200);
                } catch (InterruptedException e) {
                    // 改进
                    Thread.currentThread().interrupt();
                    e.printStackTrace();
                }
                System.out.println("t1---------hello interrupt");
            }
        }, "t1");
        t1.start();

        try {
            TimeUnit.MILLISECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(() -> {
            t1.interrupt();
        },"t2").start();
    }
}
